import axios from "axios";
import store from "@/store";
import { difTokenTime } from "@/utils/auh";

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000,
});
service.interceptors.request.use(
  (config) => {
    if (localStorage.getItem("token")) {
      if (difTokenTime()) {
        store.dispatch("logout");
        return Promise.reject(new Error("token 失效"));
      }
    }
    config.headers.token = localStorage.getItem("token");
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    const { data, status } = response;
    return data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
